<?php


namespace app\admin\controller;
use app\admin\model\VpeopleModel;
use think\Db;


class Vpeople extends Base
{

    /**

     * [index vip列表]

     * @return [type] [description]

     * @author

     */

    public function index(){

        if(request()->isAjax ()){

            extract(input());

            $map = [];

            if(isset($start)&&$start!=""&&isset($end)&&$end=="")

            {

                $map['create_time'] = ['>= time',$start];

            }

            if(isset($end)&&$end!=""&&isset($start)&&$start=="")

            {

                $map['create_time'] = ['<= time',$end];

            }

            if(isset($start)&&$start!=""&&isset($end)&&$end!="")

            {

                $map['create_time'] = ['between time',[$start,$end]];

            }

            if(isset($key)&&$key!="")

            {

                $map['nick_name|phone|email'] = ['like',"%" . $key . "%"];

            }

            $map['is_deldete'] = 1;

            $b_date = date('Y-m-d H:i:s');

            $vip = new VpeopleModel();

            $Nowpage = input('get.page') ? input('get.page'):1;

            $limits = input("limit")?input("limit"):10;

            $count = $vip->getVipCount($map);

            $lists = $vip->getVipsByWhere($map,$Nowpage, $limits);

            foreach ($lists as $key=>&$value){

                if ($b_date >= date('Y-m-d H:i:s',$value['lock_time'])){

                    $value['is_lock'] = 1;

                }

            }

            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);

        }

        return $this->fetch("vpeople/index");

    }

    /**

     * [del_vip 删除用户]

     * @return mixed|\think\response\Json

     */

    public function del_vip()

    {

        $id = input('param.id');

        $filmstrip = new VpeopleModel();

        $flag = $filmstrip->delVip($id);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }



    /**

     * [edit_vip 编辑用户]

     * @return mixed|\think\response\Json

     */

    public function edit_vip()

    {

        $vip = new VpeopleModel();

        if(request()->isPost()){

            $param = input('post.');

            if(empty($param['password'])){

                unset($param['password']);

            }else{

                $param['password'] = md5($param['password'] . $param['pwd_code']);

            }

            $flag = $vip->editVip($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

        }

        $id = input('param.id');

        $data = $vip->getOneVip($id);

        $this->assign('vip',$data);

        return $this->fetch('vpeople/edit_vip',['configState'=>config('qiniu_allow_state')]);

    }



    /**

     * [checkName 验证用户名称唯一性]

     * @return \think\response\Json

     */

    public function checkName(){

        extract(input());

        if(isset($id)&&$id!=""){

            $u_id = $id;

        }else{

            $u_id = '';

        }

        $user = new VpeopleModel();

        $flag =  $user->checkName ($nick_name,$u_id);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }



    /**

     * [vip_state vip状态]

     * @return [type] [description]

     * @author

     */

    public function vip_state()

    {

        extract(input());

        $role = new VpeopleModel();

        $time = \fast\Date::unixtime('day', 2);

        $flag = $role->vipState($id,$num,$time);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }



    /**

     * [usingVip 批量启用vip]

     * @return \think\response\Json

     */

    public function usingVip(){

        extract(input());

        $list = [];

        if($ids){

            $ids = explode(',',$ids);

            for($i=0;$i<count($ids);$i++){

                $param = [

                    'u_id'=>$ids[$i],

                    'is_lock'=>1

                ];

                $list[] = $param;

            }

        }

        $vip = new VpeopleModel();

        $flag = $vip->usingVip($list);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }



    /**

     * [forbiddenVip 批量禁用vip]

     * @return \think\response\Json

     */

    public function forbiddenVip(){

        extract(input());

        $list = [];

        if($ids){

            $ids = explode(',',$ids);

            for($i=0;$i<count($ids);$i++){

                $param = [

                    'u_id'=>$ids[$i],

                    'is_lock'=>2

                ];

                $list[] = $param;

            }

        }

        $vip = new VpeopleModel();

        $flag = $vip->forbiddenVip($list);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }



    /**

     * [excelVip 导出Excel]

     * @return \think\response\Json

     * @throws \PHPExcel_Exception

     * @throws \PHPExcel_Reader_Exception

     * @throws \PHPExcel_Writer_Exception

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function excelVip(){

        extract(input());

        if($ids =="" && $key == "" && $start == "" && $end == ""){

            $data = Db::name('user')->select();

        }

        if($ids != ""){

            $ids = trim($ids,',');

            $ids = explode(',',$ids);

            $data = Db::name('user')->where('u_id','in',$ids)->select();

        }else{

            $map = [];

            if($key!="")

            {

                $map['nick_name|phone|email'] = ['like',"%" . $key . "%"];

            }

            if($start!=""&&$end=="")

            {

                $map['last_login_time'] = ['>= time',$start];

            }

            if($end!=""&&$start=="")

            {

                $map['last_login_time'] = ['<= time',$end];

            }

            if($start!=""&&$end!="")

            {

                $map['last_login_time'] = ['between time',[$start,$end]];

            }

            $data = Db::name('user')->where($map)->select();

        }

        foreach ($data as $key=>&$value){

            $value['last_login_time'] = date("Y-m-d H:i",$value['last_login_time']);

            $value['reg_time'] = date("Y-m-d H:i",$value['reg_time']);

            if ($value['sex'] == 1){

                $value['sex'] = "男";

            }else{

                $value['sex'] = "女";

            }

            if ($value['is_lock'] == 1){

                $value['is_lock'] = "未锁定";

            }else{

                $value['is_lock'] = "以锁定";

            }

            if ($value['user_status'] == 1){

                $value['user_status'] = "离线";

            }else{

                $value['user_status'] = "在线";

            }

        }

        $cellname = [

            ['u_id','ID',15,'LEFT'],

            ['nick_name','用户昵称',15,'LEFT'],

            ['sex','用户性别',15,'LEFT'],

            ['phone','用户手机号',15,'LEFT'],

            ['email','用户邮箱',15,'LEFT'],

            ['signature','用户个性签名',15,'LEFT'],

            ['head_img','用户头像',20,'LEFT'],

            ['login_num','用户登录次数',15,'LEFT'],

            ['last_login_ip','上次登录ip',15,'LEFT'],

            ['last_login_time','上次登录时间',15,'LEFT'],

            ['user_status','用户登陆状态',15,'LEFT'],

            ['is_lock','用户权限状态',15,'LEFT'],

            ['reg_time','用户注册时间',15,'LEFT'],

        ];

        $res = exportExcel('vip信息','user',$cellname,$data);

        return json($res);

    }



    /**详情页

     * @return mixed

     */

    public function view_details(){

        $id = input('param.id');

        $vip = new VpeopleModel();

        $data = $vip->getOneVip($id)->toArray();

        $list = array(

            "u_id" => $data['u_id'],

            "用户名" => $data['nick_name'],

            "邮箱" => $data['email'],

            "手机号" => $data['phone'],

            "个性签名" => $data['signature'],

            "性别" => $data['sex']==1?"男":"女",

            "头像" => $data['head_img'],

            "生日" => date("Y-m-d",$data['birthday']),

            "注册ip" => $data['reg_ip'],

            "最后登录ip" => $data['last_login_ip'],

            "微信唯一标识" => $data['wx_openid'],

            "微信登录标识" => $data['parameter'],

            "锁定状态" => $data['is_lock']==1?"未锁定":"已锁定",

            "积分" => $data['integral'],

            "登陆次数" => $data['login_num'],

            "登录状态" => $data['user_status']==1?"在线":"离线",

            "注册时间" => date("Y-m-d H:i",$data['reg_time']),

            "最后登录时间" => date("Y-m-d H:i",$data['last_login_time']),

        );

        return $this->fetch('vpeople/detail',['info' => $list]);

    }

}